package com.hailiao.beidou.message.ui.page.contacts

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.d10ng.common.calculate.keep
import com.d10ng.common.calculate.numberRegex
import com.d10ng.compose.ui.AppColor
import com.d10ng.compose.ui.AppShape
import com.d10ng.compose.ui.PageTransitions
import com.d10ng.compose.ui.base.Button
import com.d10ng.compose.ui.base.ButtonSize
import com.d10ng.compose.ui.base.ButtonType
import com.d10ng.compose.ui.base.Cell
import com.d10ng.compose.ui.base.CellGroup
import com.d10ng.compose.ui.form.Field
import com.d10ng.compose.ui.navigation.NavBar
import com.hailiao.beidou.message.data.db.entity.ContactEntity
import com.hailiao.beidou.message.utils.back
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import com.ramcosta.composedestinations.navigation.DestinationsNavigator

/**
 * 联系人详情
 * @Author d10ng
 * @Date 2024/3/2 16:40
 */
@RootNavGraph
@Destination(
    style = PageTransitions::class,
    navArgsDelegate = ContactDetailScreenNavArgs::class
)
@Composable
fun ContactDetailScreen(
    model: ContactDetailScreenViewModel = viewModel()
) {
    val isEdit by model.isEditFlow.collectAsState()
    val info by model.infoFlow.collectAsState()

    ContactDetailScreenView(
        isEdit = isEdit,
        info = info,
        onUpdateInfo = model::updateInfo,
        onClickSave = model::onClickSave,
        onClickSend = model::onClickSend,
        onClickDelete = model::onClickDelete
    )
}

@Composable
private fun ContactDetailScreenView(
    isEdit: Boolean = false,
    info: ContactEntity = ContactEntity(),
    onUpdateInfo: (ContactEntity) -> Unit = {},
    onClickSave: () -> Unit = {},
    onClickSend: () -> Unit = {},
    onClickDelete: () -> Unit = {}
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(AppColor.Neutral.bg)
            .navigationBarsPadding()
    ) {
        NavBar(
            title = "${if (isEdit) "编辑" else "添加"}联系人",
            titleAlignment = Alignment.CenterStart,
            onClickBack = { back() }
        ) {
            if (isEdit) {
                Button(
                    text = "删除",
                    type = ButtonType.DANGER,
                    size = ButtonSize.MINI,
                    plain = true,
                    onClick = onClickDelete,
                    modifier = Modifier.padding(end = 19.dp)
                )
            }
        }
        Column(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f)
                .verticalScroll(rememberScrollState())
        ) {
            CellGroup(
                border = false,
                modifier = Modifier.padding(top = 12.dp)
            ) {
                Field(
                    value = info.number,
                    onValueChange = {
                        onUpdateInfo(info.copy(number = it.keep(numberRegex)))
                    },
                    label = "北斗/手机号",
                    type = KeyboardType.Number,
                    canClear = true,
                    disabled = isEdit,
                    required = true
                )
                Field(
                    value = info.name,
                    onValueChange = {
                        onUpdateInfo(info.copy(name = it.trim().take(20)))
                    },
                    label = "姓名",
                    type = KeyboardType.Text,
                    canClear = true,
                    required = true
                )
                Field(
                    value = info.note,
                    onValueChange = {
                        onUpdateInfo(info.copy(note = it.keep(numberRegex).take(50)))
                    },
                    label = "备注",
                    type = KeyboardType.Number,
                    canClear = true
                )
                Cell(title = "注意", label = "北二卡号请输入7位数字，不足7位前面记得补0；\n北三卡号请输入8位数字，不足8位的前面记得补0；\n手机号目前仅支持国内11位手机号；")
            }
            Button(
                text = "保存",
                type = ButtonType.PRIMARY,
                size = ButtonSize.BIG,
                shape = AppShape.RC.Cycle,
                onClick = onClickSave,
                modifier = Modifier
                    .padding(horizontal = 100.dp)
                    .padding(top = 50.dp)
                    .fillMaxWidth()
            )
            Button(
                text = "发送信息",
                plain = true,
                size = ButtonSize.BIG,
                shape = AppShape.RC.Cycle,
                onClick = onClickSend,
                modifier = Modifier
                    .padding(horizontal = 100.dp)
                    .padding(top = 25.dp)
                    .fillMaxWidth()
            )
        }
    }
}

@Preview
@Composable
private fun ContactDetailScreenPreview() {
    ContactDetailScreenView()
}